'''
FilePath: \hart\test\pkg\entity.py
Author: shenghao.xu
Date: 2023-03-29 22:18:08
LastModifiedBy: shenghao.xu
LastEditTime: 2023-12-21 14:56:40
Descripttion:
'''
import binascii
from ctypes import *
import datetime
import sys
import os
import numpy as np

sys.path.append(os.path.dirname(os.path.abspath(__file__)))  # 添加项目路径
from common import print_hex_data_space, bytes_to_ctypes, str_to_ascii, copy_to_array, convert_timestrap, encode_ascii_6, bytes2float32slice  # noqa

response_func = CFUNCTYPE(c_void_p, c_ubyte, POINTER(c_ubyte), c_ushort)
flash_read_func = CFUNCTYPE(c_char_p, c_uint32, POINTER(c_ubyte), c_ushort)
flash_write_func = CFUNCTYPE(c_char_p, c_uint32, POINTER(c_ubyte), c_ushort)
perform_self_test_func = CFUNCTYPE(c_void_p)
device_reset_func = CFUNCTYPE(c_void_p)
squawk_control_func = CFUNCTYPE(c_void_p, c_bool, c_uint8)
armed_func = CFUNCTYPE(c_char_p)


class hart_interface_t(Structure):
    _fields_ = [
        ("response", response_func),
        ("flash_read", flash_read_func),
        ("flash_write", flash_write_func),
        ("perform_self_test", perform_self_test_func),
        ("device_reset", device_reset_func),
        ("squawk_control", squawk_control_func),
        ("armed", armed_func),
    ]
    pass


class hart_master_init_t(Structure):  # 模块主机初始化接口参数
    _pack_ = 1  # 1字节对齐
    # address_bit 为1时表示基本主设备，为0时表示副主设备
    _fields_ = [("address_bit", c_ubyte)]
    pass


class hart_slave_init_t(Structure):  # 模块从机初始化接口参数
    _pack_ = 1  # 1字节对齐
    _fields_ = []
    pass


class hart_command_none_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = []
    pass


class hart_command_6_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("poll_address", c_ubyte), ("loop_current_mode", c_ubyte)]
    pass


class hart_command_11_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("tag", c_ubyte * 6)]
    pass


class hart_command_17_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("message", c_ubyte * 24)]
    pass


class hart_command_18_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("tag", c_ubyte * 6), ("descriptor",
                                       c_ubyte * 12), ("date", c_ubyte * 3)]
    pass


class hart_command_19_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("final_assembly_number", c_ubyte * 3)]
    pass


class hart_command_21_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("long_tag", c_ubyte * 32)]
    pass


class hart_command_22_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("long_tag", c_ubyte * 32)]
    pass


class hart_command_38_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("configuration_change_counter", c_uint16)]
    pass


class hart_command_33_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("slot_0", c_uint8), ("slot_1", c_uint8),
                ("slot_2", c_uint8), ("slot_3", c_uint8)]
    pass


class hart_command_34_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_damping_value", c_float)]
    pass


class hart_command_35_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_upper_and_lower_range_values_units_code", c_uint8), ("pv_upper_range_value", c_float),
                ("pv_lower_range_value", c_float)]
    pass


class hart_command_39_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("control_code", c_uint8)]
    pass


class hart_command_40_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_fixed_current_level", c_float)]
    pass


class hart_command_44_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_units_code", c_uint8)]
    pass


class hart_command_45_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_loop_current_level", c_float)]
    pass


class hart_command_46_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_loop_current_level", c_float)]
    pass


class hart_command_47_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_transfer_function_code", c_uint8)]
    pass


class hart_command_49_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_transducer_serial_number", c_uint8 * 3)]
    pass


class hart_command_51_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariables", c_uint8 * 4)]
    pass


class hart_command_52_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8)]
    pass


class hart_command_53_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8),
                ("device_ariable_unit_code", c_uint8)]
    pass


class hart_command_54_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8)]
    pass


class hart_command_55_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8), ("damping_value", c_float)]
    pass


class hart_command_56_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8), ("serial_number", c_uint8 * 3)]
    pass


class hart_command_58_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("unit_tag", c_uint8 * 6), ("unit_descriptor",
                                            c_uint8 * 12), ("unit_date", c_uint8 * 3)]
    pass


class hart_command_59_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("s2m_preambles", c_uint8)]
    pass


class hart_command_60_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8)]
    pass


class hart_command_62_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("slot0", c_uint8), ("slot1", c_uint8),
                ("slot2", c_uint8), ("slot3", c_uint8)]
    pass


class hart_command_63_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8)]
    pass


class hart_command_64_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8),
                ("damping_value", c_float)]
    pass


class hart_command_65_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8), ("upper_and_lower_range_values_units_code", c_uint8),
                ("upper_range_value", c_float), ("lower_range_value", c_float)]
    pass


class hart_command_66_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8), ("units_code", c_uint8),
                ("fixed_analog_channel_level", c_float)]
    pass


class hart_command_67_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8), ("units_code", c_uint8),
                ("actual_analog_channel_level", c_float)]
    pass


class hart_command_68_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8), ("units_code", c_uint8),
                ("externally_measured_analog_channel_level", c_float)]
    pass


class hart_command_69_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8),
                ("transfer_function_code", c_uint8),]
    pass


class hart_command_70_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("analog_channel_number_code", c_uint8)]
    pass


class hart_command_71_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("lock_code", c_uint8)]
    pass


class hart_command_72_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("squawk_control_code", c_uint8)]
    pass


class hart_command_75_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("card", c_uint8), ("channel", c_uint8),
                ("poll_address", c_uint8)]
    pass


class hart_command_78_command_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("command", c_uint16),
                ("count", c_uint8), ("payload", c_uint8)]
    pass


class hart_command_78_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("number", c_uint8), ("command", hart_command_78_command_t)]
    pass


class hart_command_79_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8), ("device_ariable_command_code", c_uint8), ("device_ariable_unit_code", c_uint8),
                ("device_ariable_value", c_float), ("status", c_uint8)]
    pass


class hart_command_80_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8)]
    pass


class hart_command_81_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8)]
    pass


class hart_command_82_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8), ("trim_points", c_uint8),
                ("units_code", c_uint8), ("value", c_float)]
    pass


class hart_command_83_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("device_ariable", c_uint8)]
    pass


class hart_command_84_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("identifier", c_uint16)]
    pass


class hart_command_85_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("card", c_uint8), ("channel", c_uint8)]
    pass


class hart_command_86_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("identifier", c_uint16)]
    pass


class hart_command_87_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("master_mode", c_uint8)]
    pass


class hart_command_88_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("retry_count", c_uint8)]
    pass


class hart_command_89_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("time_set_code", c_uint8), ("date", c_uint8 * 3),
                ("time", c_uint32), ("transmission_time", c_uint16)]
    pass


class hart_command_91_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("trend_number", c_uint8)]
    pass


class hart_command_92_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("trend_number", c_uint8), ("trend_code", c_uint8),
                ("device_variable_code", c_uint8), ("trend_sample_interval", c_uint32)]
    pass


class hart_command_93_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("trend_number", c_uint8)]
    pass


class hart_command_96_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("action_number", c_uint8)]
    pass


class hart_command_97_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("action_number", c_uint8), ("action_control_code", c_uint8), ("device_variable_code",
                                                                               c_uint8), ("command_number", c_uint16), ("trigger_date", c_uint8*3), ("trigger_time", c_uint32)]
    pass


class hart_command_98_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("action_number", c_uint8)]
    pass


class hart_command_99_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("action_number", c_uint8), ("command_number", c_uint16),
                ("command_data_length", c_uint8), ("command_data", c_uint8*255)]
    pass


class hart_command_100_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("pv_alarm_selection_code", c_uint8)]
    pass


class hart_command_101_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("burst_message", c_uint8)]
    pass


class hart_command_102_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("burst_message", c_uint8), ("sub_device_index", c_uint16)]
    pass


class hart_command_103_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("burst_message", c_uint8), ("update_period_time",
                                             c_uint32), ("max_update_period_time", c_uint32)]
    pass


class hart_command_104_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("burst_message", c_uint8), ("burst_message_trigger_code", c_uint8),
                ("device_variable_vlassification_for_trigger_level", c_uint8), ("units_code", c_uint8), ("trigger_level", c_float)]
    pass


class hart_command_105_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("burst_message", c_uint8)]
    pass


class hart_command_107_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("solts", c_uint8*8), ("burst_message", c_uint8)]
    pass


class hart_command_513_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("country_code", c_uint16), ("si_units_control_code", c_uint8)]
    pass


class hart_command_130_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("code", c_uint8*8),
                ]
    pass


class hart_command_141_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("write_protect_code", c_uint8),
                ]
    pass


class hart_command_254_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("model", c_uint8)]
    pass


class hart_command_260_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [
        ("model", c_uint8),
        ("threshold_control", c_float),
        ("amplifier_circuit_control", c_float),
        ("pressure_A", c_uint16),
        ("pressure_B", c_uint16),
        ("position_selector", c_uint8),
        ("position_feedback_P", c_float),
        ("position_feedback_I", c_float),
        ("position_feedback_D", c_float),
        ("position_feedback_amplifier_circuit_control_gain", c_uint16),
        ("pressure_selector", c_uint8),
        ("pressure_feedback_P", c_float),
        ("pressure_feedback_I", c_float),
        ("pressure_feedback_D", c_float),
        ("pressure_feedback_amplifier_circuit_control_gain", c_uint16),
        ("valve_model", c_uint8*32),
        ("spring_coefficient", c_float),
        ("gas_inlet_cross_sectional_area", c_float),
        ("gas_opening_closing", c_uint8),
    ]
    pass


class hart_command_500_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("test_type", c_uint8), ("data_length", c_uint8),
                ("data", c_uint8*32),]
    pass


class hart_command_500_rtc_data_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("year", c_uint8),
                ("month", c_uint8),
                ("day", c_uint8),
                ("weekday", c_uint8),
                ("hour", c_uint8),
                ("minute", c_uint8),
                ("second", c_uint8)
                ]

    def get_bytes(self):
        return bytes(self)
    pass


class hart_command_500_u16_data_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("data", c_uint16),
                ]

    def get_bytes(self):
        return bytes(self)
    pass


class hart_master_command_req_data_u(Union):  # 模块初始化接口参数
    _pack_ = 1  # 1字节对齐
    _fields_ = [("command999", hart_command_none_t),
                ("command6", hart_command_6_t),
                ("command11", hart_command_11_t),
                ("command17", hart_command_17_t),
                ("command18", hart_command_18_t),
                ("command19", hart_command_19_t),
                ("command21", hart_command_21_t),
                ("command22", hart_command_22_t),
                ("command38", hart_command_38_t),
                ("command33", hart_command_33_t),
                ("command34", hart_command_34_t),
                ("command35", hart_command_35_t),
                ("command39", hart_command_39_t),
                ("command40", hart_command_40_t),
                ("command44", hart_command_44_t),
                ("command45", hart_command_45_t),
                ("command46", hart_command_46_t),
                ("command47", hart_command_47_t),
                ("command49", hart_command_49_t),
                ("command51", hart_command_51_t),
                ("command52", hart_command_52_t),
                ("command53", hart_command_53_t),
                ("command54", hart_command_54_t),
                ("command55", hart_command_55_t),
                ("command56", hart_command_56_t),
                ("command58", hart_command_58_t),
                ("command59", hart_command_59_t),
                ("command60", hart_command_60_t),
                ("command62", hart_command_62_t),
                ("command63", hart_command_63_t),
                ("command64", hart_command_64_t),
                ("command65", hart_command_65_t),
                ("command66", hart_command_66_t),
                ("command67", hart_command_67_t),
                ("command68", hart_command_68_t),
                ("command69", hart_command_69_t),
                ("command70", hart_command_70_t),
                ("command71", hart_command_71_t),
                ("command72", hart_command_72_t),
                ("command75", hart_command_75_t),
                ("command79", hart_command_79_t),
                ("command80", hart_command_80_t),
                ("command81", hart_command_81_t),
                ("command82", hart_command_82_t),
                ("command83", hart_command_83_t),
                ("command84", hart_command_84_t),
                ("command85", hart_command_85_t),
                ("command86", hart_command_86_t),
                ("command87", hart_command_87_t),
                ("command88", hart_command_88_t),
                ("command89", hart_command_89_t),
                ("command91", hart_command_91_t),
                ("command92", hart_command_92_t),
                ("command93", hart_command_93_t),
                ("command96", hart_command_96_t),
                ("command97", hart_command_97_t),
                ("command98", hart_command_98_t),
                ("command99", hart_command_99_t),
                ("command100", hart_command_100_t),
                ("command101", hart_command_101_t),
                ("command102", hart_command_102_t),
                ("command103", hart_command_103_t),
                ("command104", hart_command_104_t),
                ("command105", hart_command_105_t),
                ("command107", hart_command_107_t),
                ("command513", hart_command_513_t),
                ("command130", hart_command_130_t),
                ("command132", hart_command_130_t),
                ("command141", hart_command_141_t),
                ("command254", hart_command_254_t),
                ("command260", hart_command_260_t),
                ("command500", hart_command_500_t),
                ]
    pass


def get_master_struct():
    hart = hart_init_t()
    hart.dir = 1
    hart.master_address_bit = 1
    hart.hart_interface_t.response = response_func(hart.hart_response_call)
    return hart


def get_slave_struct():
    hart = hart_init_t()
    hart.dir = 2
    hart.hart_interface_t.response = response_func(hart.hart_response_call)
    hart.hart_interface_t.flash_read = flash_read_func(hart.flash_read)
    hart.hart_interface_t.flash_write = flash_write_func(hart.flash_write)
    hart.hart_interface_t.perform_self_test = perform_self_test_func(
        hart.perform_self_test)
    hart.hart_interface_t.device_reset = device_reset_func(hart.device_reset)
    hart.hart_interface_t.squawk_control = squawk_control_func(
        hart.squawk_control)
    hart.hart_interface_t.armed = armed_func(hart.armed)
    return hart


class hart_rsp_t(Structure):
    _pack_ = 1  # 1字节对齐
    _fields_ = [("address", c_ubyte*5),
                ("address_length", c_ubyte),
                ("command", c_uint32),
                ("data_length", c_ubyte),
                ("data", c_ubyte*254),
                ]

    def get_bytes(self):
        return bytes(self)
    pass


class hart_init_t(Structure):  # 模块初始化接口参数
    _pack_ = 1  # 1字节对齐
    _fields_ = [("hart_protocol_version", c_ubyte),
                ("dir", c_ubyte),
                ("master_address_bit", c_ubyte),
                ("hart_interface_t", hart_interface_t),
                ]
    response = []

    def __init__(self):
        self.hart_protocol_version = 7
        pass

    def hart_response_call(self, index, ptr, length):
        self.response = ptr[:length]
        hart_response_call(index, ptr, length)
        pass

    def flash_read(self, addr, ptr, length):
        print("flash_read:", addr, ptr, length)
        return True

    def flash_write(self, addr, ptr, length):
        print("flash_write:", addr, ptr, length)
        return True

    def perform_self_test(self):
        print("自检结束")
        pass

    def device_reset(self):
        print("设备复位")
        pass

    def squawk_control(self, open, second):
        print("squawk_control", open, second)

    def armed(self):
        print("armed")
        return True
    pass


class hart_command_req_t(Structure):  # 主机请求指令接口参数
    _pack_ = 1  # 1字节对齐
    _anonymous_ = ("data",)
    _fields_ = [("delimiter_type", c_ubyte),
                # 短帧地址1个字节，长帧地址5个字节，上位机发送时从低位到高位填充，例如短地址：0x01，长地址：0x00 0x00 0x00 0x00 0x01
                ("address", c_ubyte * 5),
                ("command", c_uint32),
                ("data", hart_master_command_req_data_u)]

    def __init__(self):
        self._address = (0, 0, 0, 0, 0)
        self.command = 0

        self.set_long_address()  # 界定符类型： 0：短帧 1：长帧  默认1
        pass

    def set_short_address(self):
        self.delimiter_type = 0
        self.address = self._address[::-1]
        pass

    def set_long_address(self):
        self.delimiter_type = 1
        self.address = self._address
        pass

    def set_address(self, address="0000000000"):
        self._address = bytes_to_ctypes(bytearray.fromhex(address))
        type(self._address)
        self.address = self._address
        pass

    def set_command(self, cmd):
        self.command = cmd
        pass

    def get_bytes(self):
        return bytes(self)

    pass


def get_master_command6_req_struct():
    req = hart_command_req_t()
    req.set_command(6)
    req.data.command6.poll_address = 1  # 轮询地址
    req.data.command6.loop_current_mode = 1  # 回路电流模式 0:关闭 1:开启
    return req


def get_master_command11_req_struct():
    req = hart_command_req_t()
    req.set_command(11)
    tag = "lapool"
    copy_to_array(req.data.command11.tag, str_to_ascii(tag), 0)
    return req


def get_master_command17_req_struct():
    req = hart_command_req_t()
    req.set_command(17)
    msg = "2023-05-11 14:02:00"
    hex = str_to_ascii(msg)
    bs = [0x20]*32
    copy_to_array(bs, hex, 0)
    encode_ascii_6(bs, 32, req.data.command17.message)
    return req


def get_master_command18_req_struct():
    req = hart_command_req_t()
    req.set_command(18)

    tag = "LAPOOL"
    descriptor = "HART-7"
    date = (2023 - 1900, 5, 11)

    bs = [0x20]*24
    hex = str_to_ascii(tag)
    copy_to_array(bs, hex, 0)
    encode_ascii_6(bs, 8, req.data.command18.tag)

    bs = [0x20]*24
    hex = str_to_ascii(descriptor)
    copy_to_array(bs, hex, 0)
    encode_ascii_6(bs, 16, req.data.command18.descriptor)

    copy_to_array(req.data.command18.date, date, 0)
    return req


def get_master_command19_req_struct():
    req = hart_command_req_t()
    req.set_command(19)
    s = (0xA0, 0, 1)
    copy_to_array(req.data.command19.final_assembly_number, s, 0)
    return req


def get_master_command21_req_struct():
    req = hart_command_req_t()
    req.set_command(21)
    tag = "lapool-long"
    copy_to_array(req.data.command21.long_tag, str_to_ascii(tag), 0)
    return req


def get_master_command22_req_struct():
    req = hart_command_req_t()
    req.set_command(22)
    tag = "lapool-long-2"
    copy_to_array(req.data.command22.long_tag, str_to_ascii(tag), 0)
    return req


def get_master_command38_req_struct():
    req = hart_command_req_t()
    req.set_command(38)
    req.data.command38.configuration_change_counter = 2
    return req


def get_master_command33_req_struct():
    req = hart_command_req_t()
    req.set_command(33)
    req.data.command33.slot_0 = 10
    req.data.command33.slot_1 = 243
    req.data.command33.slot_2 = 245
    req.data.command33.slot_3 = 246
    return req


def get_master_command34_req_struct():
    req = hart_command_req_t()
    req.set_command(34)
    req.data.command34.pv_damping_value = 10.0
    return req


def get_master_command35_req_struct():
    req = hart_command_req_t()
    req.set_command(35)
    req.data.command35.pv_upper_and_lower_range_values_units_code = 32
    req.data.command35.pv_upper_range_value = 20
    req.data.command35.pv_lower_range_value = 4
    return req


def get_master_command39_req_struct():
    req = hart_command_req_t()
    req.set_command(39)
    req.data.command39.control_code = 0
    return req


def get_master_command40_req_struct():
    req = hart_command_req_t()
    req.set_command(40)
    req.data.command40.pv_fixed_current_level = 10.0
    return req


def get_master_command44_req_struct():
    req = hart_command_req_t()
    req.set_command(44)
    req.data.command44.pv_units_code = 33
    return req


def get_master_command45_req_struct():
    req = hart_command_req_t()
    req.set_command(45)
    req.data.command45.pv_loop_current_level = 4.0
    return req


def get_master_command46_req_struct():
    req = hart_command_req_t()
    req.set_command(46)
    req.data.command46.pv_loop_current_level = 20.0
    return req


def get_master_command47_req_struct():
    req = hart_command_req_t()
    req.set_command(47)
    req.data.command47.pv_transfer_function_code = 1
    return req


def get_master_command49_req_struct():
    req = hart_command_req_t()
    req.set_command(49)
    req.data.command49.pv_transducer_serial_number = (0xa0, 0x00, 0x01)
    return req


def get_master_command51_req_struct():
    req = hart_command_req_t()
    req.set_command(51)
    req.data.command51.device_ariables = (0x01, 0x02, 0x03, 250)
    return req


def get_master_command52_req_struct():
    req = hart_command_req_t()
    req.set_command(52)
    req.data.command52.device_ariable = 246
    return req


def get_master_command53_req_struct():
    req = hart_command_req_t()
    req.set_command(53)
    req.data.command53.device_ariable = 246
    req.data.command53.device_ariable_unit_code = 35
    return req


def get_master_command54_req_struct():
    req = hart_command_req_t()
    req.set_command(54)
    req.data.command54.device_ariable = 246
    return req


def get_master_command55_req_struct():
    req = hart_command_req_t()
    req.set_command(55)
    req.data.command55.device_ariable = 246
    req.data.command55.damping_value = 1
    return req


def get_master_command56_req_struct():
    req = hart_command_req_t()
    req.set_command(56)
    req.data.command56.device_ariable = 246
    req.data.command56.serial_number = (0xa0, 0x00, 0x01)
    return req


def get_master_command58_req_struct():
    req = hart_command_req_t()
    req.set_command(58)
    tag = "tm001"
    descriptor = "temperature"
    date = (17, 5, 2023 - 1900)
    copy_to_array(req.data.command58.unit_tag, str_to_ascii(tag), 0)
    copy_to_array(req.data.command58.unit_descriptor,
                  str_to_ascii(descriptor), 0)
    copy_to_array(req.data.command58.unit_date, date, 0)
    return req


def get_master_command59_req_struct():
    req = hart_command_req_t()
    req.set_command(59)
    req.data.command59.s2m_preambles = 7
    return req


def get_master_command60_req_struct():
    req = hart_command_req_t()
    req.set_command(60)
    req.data.command60.analog_channel_number_code = 0
    return req


def get_master_command62_req_struct():
    req = hart_command_req_t()
    req.set_command(62)
    req.data.command62.slot0 = 0
    req.data.command62.slot1 = 1
    req.data.command62.slot2 = 2
    req.data.command62.slot3 = 3
    return req


def get_master_command63_req_struct():
    req = hart_command_req_t()
    req.set_command(63)
    req.data.command63.analog_channel_number_code = 0
    return req


def get_master_command64_req_struct():
    req = hart_command_req_t()
    req.set_command(64)
    req.data.command64.analog_channel_number_code = 0
    req.data.command64.damping_value = 1.1
    return req


def get_master_command65_req_struct():
    req = hart_command_req_t()
    req.set_command(65)
    req.data.command65.analog_channel_number_code = 0
    req.data.command65.upper_and_lower_range_values_units_code = 32
    req.data.command65.upper_range_value = 20
    req.data.command65.lower_range_value = 4
    return req


def get_master_command66_req_struct():
    req = hart_command_req_t()
    req.set_command(66)
    req.data.command66.analog_channel_number_code = 0
    req.data.command66.units_code = 32
    bys = np.array([0x7f, 0xa0, 0x00, 0x00], dtype=np.uint8)
    bs = bytes2float32slice(bys)
    # req.data.command66.fixed_analog_channel_level = 1.2

    req.data.command66.fixed_analog_channel_level = bs[0]
    return req


def get_master_command67_req_struct():
    req = hart_command_req_t()
    req.set_command(67)
    req.data.command67.analog_channel_number_code = 0
    req.data.command67.units_code = 32
    req.data.command67.actual_analog_channel_level = 1.2
    return req


def get_master_command68_req_struct():
    req = hart_command_req_t()
    req.set_command(68)
    req.data.command68.analog_channel_number_code = 0
    req.data.command68.units_code = 32
    req.data.command68.externally_measured_analog_channel_level = 1.2
    return req


def get_master_command69_req_struct():
    req = hart_command_req_t()
    req.set_command(69)
    req.data.command69.analog_channel_number_code = 0
    req.data.command69.transfer_function_code = 1
    return req


def get_master_command70_req_struct():
    req = hart_command_req_t()
    req.set_command(70)
    req.data.command70.analog_channel_number_code = 0
    return req


def get_master_command71_req_struct():
    req = hart_command_req_t()
    req.set_command(71)
    req.data.command71.lock_code = 0
    return req


def get_master_command72_req_struct():
    req = hart_command_req_t()
    req.set_command(72)
    req.data.command72.squawk_control_code = 0
    return req


def get_master_command75_req_struct():
    req = hart_command_req_t()
    req.set_command(75)
    req.set_address("0000000000")
    req.data.command75.card = 0
    req.data.command75.channel = 0
    req.data.command75.poll_address = 0
    return req


def get_master_command79_req_struct():
    req = hart_command_req_t()
    req.set_command(79)
    req.data.command79.device_ariable = 6
    req.data.command79.device_ariable_command_code = 0
    req.data.command79.device_ariable_unit_code = 32
    req.data.command79.device_ariable_value = 25
    req.data.command79.status = 0
    return req


def get_master_command80_req_struct():
    req = hart_command_req_t()
    req.set_command(80)
    req.data.command80.device_ariable = 246
    return req


def get_master_command81_req_struct():
    req = hart_command_req_t()
    req.set_command(81)
    req.data.command81.device_ariable = 246
    return req


def get_master_command82_req_struct():
    req = hart_command_req_t()
    req.set_command(82)
    req.data.command82.device_ariable = 246
    req.data.command82.trim_points = 3
    req.data.command82.units_code = 32
    req.data.command82.value = 1.2
    return req


def get_master_command83_req_struct():
    req = hart_command_req_t()
    req.set_command(83)
    req.data.command83.device_ariable = 246
    return req


def get_master_command84_req_struct():
    req = hart_command_req_t()
    req.set_command(84)
    req.set_address("0000000000")
    req.data.command84.identifier = 0xA001
    return req


def get_master_command85_req_struct():
    req = hart_command_req_t()
    req.set_command(85)
    req.set_address("0000000000")
    req.data.command85.card = 0
    req.data.command85.channel = 0
    return req


def get_master_command86_req_struct():
    req = hart_command_req_t()
    req.set_command(86)
    req.set_address("0000000000")
    req.data.command86.identifier = 0xA001
    return req


def get_master_command87_req_struct():
    req = hart_command_req_t()
    req.set_command(87)
    req.set_address("0000000000")
    req.data.command87.master_mode = 1
    return req


def get_master_command88_req_struct():
    req = hart_command_req_t()
    req.set_command(88)
    req.set_address("0000000000")
    req.data.command88.retry_count = 3
    return req


def get_master_command89_req_struct():
    req = hart_command_req_t()
    req.set_command(89)
    date = (31, 5, 2023 - 1900)
    req.data.command89.time_set_code = 0
    copy_to_array(req.data.command89.date, date, 0)
    req.data.command89.time = convert_timestrap(15, 0, 1, 999)
    req.data.command89.transmission_time = 0

    return req


def get_master_command91_req_struct():
    req = hart_command_req_t()
    req.set_command(91)
    req.data.command91.trend_number = 0
    return req


def get_master_command92_req_struct():
    req = hart_command_req_t()
    req.set_command(92)
    req.data.command92.trend_number = 0
    req.data.command92.trend_code = 0
    req.data.command92.device_variable_code = 32
    req.data.command92.trend_sample_interval = 3600000+320
    return req


def get_master_command93_req_struct():
    req = hart_command_req_t()
    req.set_command(93)
    req.data.command93.trend_number = 0
    return req


def get_master_command96_req_struct():
    req = hart_command_req_t()
    req.set_command(96)
    req.data.command96.action_number = 0
    return req


def get_master_command97_req_struct():
    req = hart_command_req_t()
    req.set_command(97)
    date = (31, 5, 2024 - 1900)
    ts = (12 * 3600 + 1 * 60 + 0) * 1000
    req.data.command97.action_number = 0
    req.data.command97.action_control_code = 0x10
    req.data.command97.device_variable_code = 246
    req.data.command97.command_number = 0xffff
    copy_to_array(req.data.command97.trigger_date, date, 0)
    req.data.command97.trigger_time = ts
    return req


def get_master_command98_req_struct():
    req = hart_command_req_t()
    req.set_command(98)
    req.data.command98.action_number = 0
    return req


def get_master_command99_req_struct():
    req = hart_command_req_t()
    req.set_command(99)
    command_data_length = 10
    req.data.command99.action_number = 0
    req.data.command99.command_number = 1
    req.data.command99.command_data_length = command_data_length
    array = np.arange(command_data_length)
    copy_to_array(req.data.command99.command_data, array, 0)
    return req


def get_master_command100_req_struct():
    req = hart_command_req_t()
    req.set_command(100)
    req.data.command100.pv_alarm_selection_code = 239
    return req


def get_master_command101_req_struct():
    req = hart_command_req_t()
    req.set_command(101)
    req.data.command101.burst_message = 0
    return req


def get_master_command102_req_struct():
    req = hart_command_req_t()
    req.set_command(102)
    req.data.command102.burst_message = 0
    req.data.command102.sub_device_index = 0
    return req


def get_master_command103_req_struct():
    req = hart_command_req_t()
    ts = 3600*1000
    req.set_command(103)
    req.data.command103.burst_message = 0
    req.data.command103.update_period_time = ts
    req.data.command103.max_update_period_time = ts
    return req


def get_master_command104_req_struct():
    req = hart_command_req_t()
    req.set_command(104)
    req.data.command104.burst_message = 0
    req.data.command104.burst_message_trigger_code = 1
    req.data.command104.device_variable_vlassification_for_trigger_level = 0x40
    req.data.command104.units_code = 0x20
    req.data.command104.trigger_level = 1.2
    return req


def get_master_command105_req_struct():
    req = hart_command_req_t()
    req.set_command(105)
    req.data.command105.burst_message = 0
    return req


def get_master_command107_req_struct():
    req = hart_command_req_t()
    req.set_command(107)
    req.data.command107.solts = (1, 2, 3, 4, 5, 6, 7, 8)
    req.data.command107.burst_message = 0
    return req


def get_master_command513_req_struct():
    req = hart_command_req_t()
    req.set_command(513)
    req.data.command513.country_code = 852
    req.data.command513.si_units_control_code = 1
    return req


def get_master_command254_req_struct():
    req = hart_command_req_t()
    req.set_command(254)
    req.data.command254.model = 1
    return req


def get_master_command260_req_struct():
    req = hart_command_req_t()
    req.set_command(260)
    req.data.command260.model = 1
    req.data.command260.threshold_control = 1.1
    req.data.command260.amplifier_circuit_control = 1.1
    req.data.command260.pressure_A = 100
    req.data.command260.pressure_B = 100
    req.data.command260.position_selector = 1
    req.data.command260.position_feedback_P = 0.1
    req.data.command260.position_feedback_I = 0.2
    req.data.command260.position_feedback_D = 0.3
    req.data.command260.position_feedback_amplifier_circuit_control_gain = 10
    req.data.command260.pressure_selector = 1
    req.data.command260.pressure_feedback_P = 0.1
    req.data.command260.pressure_feedback_I = 0.1
    req.data.command260.pressure_feedback_D = 0.1
    req.data.command260.pressure_feedback_amplifier_circuit_control_gain = 10
    array = np.arange(32)
    copy_to_array(req.data.command260.valve_model, array, 0)

    req.data.command260.spring_coefficient = 0.1
    req.data.command260.gas_inlet_cross_sectional_area = 0.1
    req.data.command260.gas_opening_closing = 1

    return req


def get_master_command500_req_struct(test_type, data_length, data):
    req = hart_command_req_t()
    req.set_command(500)
    req.data.command500.test_type = test_type
    req.data.command500.data_length = data_length
    if data_length > 0:
        copy_to_array(req.data.command500.data, data, 0)
    return req


# RTC写入测试
def get_master_command500_req_struct_TEST_TYPE_RTC_WRITE():
    req = hart_command_req_t()
    req.set_command(500)
    req.data.command500.test_type = 0
    req.data.command500.data_length = 7
    rtc = hart_command_500_rtc_data_t()
    rtc.year = 0x23
    rtc.month = 0x05
    rtc.day = 0x31
    rtc.hour = 0x12
    rtc.minute = 0x59
    rtc.second = 0x59
    rtc.weekday = 0x01
    copy_to_array(req.data.command500.data, rtc.get_bytes(), 0)

    return req


def get_master_command500_req_struct_TEST_TYPE_DAC():
    req = hart_command_req_t()
    req.set_command(500)
    req.data.command500.test_type = 7
    req.data.command500.data_length = 2
    u16 = hart_command_500_u16_data_t()
    u16.data = 2000
    copy_to_array(req.data.command500.data, u16.get_bytes(), 0)

    return req


def get_master_command500_req_struct_PWM():
    req = hart_command_req_t()
    req.set_command(500)
    req.data.command500.test_type = 14
    req.data.command500.data_length = 2
    u16 = hart_command_500_u16_data_t()
    u16.data = 2000
    copy_to_array(req.data.command500.data, u16.get_bytes(), 0)

    return req


def get_master_command130_req_struct(code):
    req = hart_command_req_t()
    req.set_command(130)
    req.data.command130.code = code
    return req

def get_master_command132_req_struct(code):
    req = hart_command_req_t()
    req.set_command(132)
    req.data.command132.code = code
    return req

def get_master_command141_req_struct(code):
    req = hart_command_req_t()
    req.set_command(141)
    req.data.command141.write_protect_code = code
    return req


def hart_response_call(index, ptr, length):
    bs = ptr[:length]
    print_hex_data_space(bs, True)
    pass
